#include "DashboardUI_EditUser.h"
#include "ui_dashboard_edituser.h"

#include "InfoBoxUI.h"

#include "Authentication.h"
#include "PersonDatabase.h"
#include "UserDatabase.h"

#include <QMessageBox>

DashboardUI_EditUser::DashboardUI_EditUser(QWidget *parent) :
    QWidget(parent),
    validator(this),
    ui(new Ui::DashboardUI_EditUser)
{
    ui->setupUi(this);

    Authentication *auth = Authentication::getInstance();
    User::UserType type = auth->getUser()->getUserType();

    switch (type) {
    case User::ADMIN: // Admin can create doctors or nurses
        ui->patientRadioButton->setVisible(false);
        break;
    case User::NURSE: // Admin can create patients
        ui->doctorRadioButton->setVisible(false);
        ui->nurseRadioButton->setVisible(false);
        break;
    case User::PATIENT:
        setInfo(auth->getUser()->getInfo());
        setViewOnly();
        break;
    }

    if (type != User::PATIENT)
    {
        // Set up field validator
        validator.addAllFields();
        validator.setButton(ui->submitButton);
    }

    clearFields();
}

void DashboardUI_EditUser::setInfo(PersonInfo * info)
{
    ui->firstNameField->setText(info->firstName);
    ui->middleInitialField->setText(info->middleName);
    ui->lastNameField->setText(info->lastName);

    ui->cityField->setText(info->addressCity);
    ui->streetAddressField->setText(info->addressStreet);
    ui->zipField->setText(QString::number(info->addressZip));

    int stateIndex = ui->stateComboBox->findText(info->addressState);
    ui->stateComboBox->setCurrentIndex(stateIndex);

    ui->dobField->setDate(QDate::fromString(info->birthDate, "d/M/yyyy"));
    ui->lastVisitField->setDate(QDate::fromString(info->lastVisit, "d/M/yyyy"));
}

void DashboardUI_EditUser::setViewOnly()
{
    // Hide submit button and user selection box
    ui->submitButton->hide();
    ui->userTypeGroup->hide();

    // Disable non line edit fields
    ui->stateComboBox->setEnabled(false);
    ui->dobField->setEnabled(false);
    ui->lastVisitField->setEnabled(false);

    // Get list of line edit fields
    QList<QLineEdit *> fields = this->findChildren<QLineEdit *>();

    // Disable all line edit fields
    for(int i = 0; i < fields.size(); i++)
    {
        QLineEdit *lineEdit = fields.at(i);
        lineEdit->setEnabled(false);
    }
}

void DashboardUI_EditUser::reload()
{
    User *patient = Authentication::getInstance()->getPatient();

    setInfo(patient->getInfo());
}

DashboardUI_EditUser::~DashboardUI_EditUser()
{
    delete ui;
}

void DashboardUI_EditUser::on_submitButton_clicked()
{
    // Determine user type from radio buttons
    User::UserType type;

    if (ui->doctorRadioButton->isChecked())
        type = User::DOCTOR;
    else if (ui->nurseRadioButton->isChecked())
        type = User::NURSE;
    else
        type = User::PATIENT;

    // Get username and password fields
    QString username = ui->usernameField->text();
    QString password = ui->passwordField->text();

    // Add user to database
    UserDatabase userDB;
    int userId = userDB.addUser(username, password, type);

    if (userId == UserDatabase::USER_EXISTS)
    {
        QMessageBox::warning(NULL, "Could not add user", "Username already exists.");
        return;
    }
    else if (userId == UserDatabase::FAILURE)
    {
        QMessageBox::warning(NULL, "Warning", "Unable to register user.");
        return;
    }

    // Add personal information to database
    PersonInfo info;
    info.firstName = ui->firstNameField->text();
    info.middleName = ui->middleInitialField->text();
    info.lastName = ui->lastNameField->text();

    info.addressCity = ui->cityField->text();
    info.addressState = ui->stateComboBox->itemText(ui->stateComboBox->currentIndex());
    info.addressStreet = ui->streetAddressField->text();
    info.addressZip = ui->zipField->text().toInt();

    info.birthDate = ui->dobField->text();
    info.lastVisit = ui->lastVisitField->text();

    PersonDatabase personDB;
    bool addSuccess = personDB.addPerson(&info, userId);

    if (!addSuccess)
        QMessageBox::warning(NULL, "Warning", "Could not add personal info to database.");

    new InfoBoxUI(QString("User ") + username + " added successfully", 1500, this);

    clearFields();
}

void DashboardUI_EditUser::clearFields()
{
    validator.resetFields();

    ui->dobField->setDate(QDate(QDate::currentDate().year(), 1, 1));
    ui->lastVisitField->setDate(QDate::currentDate());

    ui->stateComboBox->setCurrentIndex(0);
}
